package com.moo.aldldemo.layoutdemo;

import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Layout;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewStub;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

  private View netError;

  private TextView textView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView) findViewById(R.id.tv_main_spannable);
    showSpannbleText();

  }

  private void showSpannbleText() {
    String text = String.format("￥%.2f 门市价:￥%.2faassasasasasasasa", 18.6, 22.0);
    int z = text.indexOf("门");
    SpannableStringBuilder style = new SpannableStringBuilder(text);
    //可点击文本
    style.setSpan(new ClickableSpan() {
      @Override
      public void onClick(View widget) {
        Toast.makeText(MainActivity.this, "可点击文本", Toast.LENGTH_SHORT).show();
      }
    }, 0, 1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    //字体尺寸
    style.setSpan(new AbsoluteSizeSpan(40), 0, 1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    style.setSpan(new AbsoluteSizeSpan(30), z, text.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
    //背景颜色
    style.setSpan(new BackgroundColorSpan(getResources().getColor(R.color.colorPrimary)), z, text.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    //字体颜色
    style.setSpan(new ForegroundColorSpan(Color.RED), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    //用样式标记文本（斜体）
    style.setSpan(new StyleSpan(Typeface.BOLD), 5, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    //用删除线标记文本
    style.setSpan(new StrikethroughSpan(), 7, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    //用下划线标记文本
    style.setSpan(new UnderlineSpan(), 10, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    //用颜色标记
    style.setSpan(new ForegroundColorSpan(Color.GREEN), 10, 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    //获取Drawable资源
    Drawable d = getResources().getDrawable(R.mipmap.ic_launcher);
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    //创建ImageSpan
    ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
    //用ImageSpan替换文本
    style.setSpan(span, 18, 19, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);


    textView.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        CharSequence text = ((TextView) v).getText();
        Spannable stext = Spannable.Factory.getInstance().newSpannable(text);
        TextView widget = (TextView) v;
        int action = event.getAction();
        if (action == MotionEvent.ACTION_UP ||
          action == MotionEvent.ACTION_DOWN) {
          int x = (int) event.getX();
          int y = (int) event.getY();
          x -= widget.getTotalPaddingLeft();
          y -= widget.getTotalPaddingTop();
          x += widget.getScrollX();
          y += widget.getScrollY();
          Layout layout = widget.getLayout();
          int line = layout.getLineForVertical(y);
          int off = layout.getOffsetForHorizontal(line, x);
          ClickableSpan[] link = stext.getSpans(off, off, ClickableSpan.class);
          if (link.length != 0) {
            if (action == MotionEvent.ACTION_UP) {
              link[0].onClick(widget);
            }
            return true;
          }
        }
        return false;
      }
    });
    textView.setClickable(true);
    textView.setText(style);

    textView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Toast.makeText(MainActivity.this, "整体文本", Toast.LENGTH_SHORT).show();
      }
    });
  }

  public void netError(View view) {
    if (netError == null) {
      ViewStub viewStub = (ViewStub) findViewById(R.id.vs_main_error);
      netError = viewStub.inflate();
    } else {
      netError.setVisibility(View.VISIBLE);
    }
  }

  public void netData(View view) {
    netError.setVisibility(View.GONE);
  }

}
